package com.ncmmall.mall.controller.business.live;

import com.ncmmall.application.business.live.LiveApplication;
import com.feijin.commons.exceptions.IllegalRequestException;
import com.feijin.commons.webs.Webs;
import com.ncmmall.domain.business.live.LiveCheck;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 直播间审核
 *
 * @author likaikai
 * @date 2020-10-27 11:28:54
 */
@Controller
@RequestMapping(value = "/live/check")
@Slf4j
public class LiveCheckController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private LiveApplication liveApplication;

    /**
     * 直播间审核列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, QueryPageRequest pageRequest, LiveCheck params) {

        Map<String, Object> map = new HashMap<>();
        String hql = buildHQL(params, map);

        QueryPage<LiveCheck> page = queryChannelService.findPage(hql, pageRequest.setParams(map), LiveCheck.class);
        model.addAttribute("page", page);
        model.addAttribute("params", params);

        return "mall/live/check/list";
    }

    private String buildHQL(LiveCheck params, Map<String, Object> map) {
        StringBuilder sb = new StringBuilder("select c from LiveCheck c where 1 = 1");
        if (StringUtils.isNotBlank(params.getName())) {
            sb.append(" and c.name like :name");
            map.put("name", "%" + params.getName() + "%");
        }
        if (StringUtils.isNotBlank(params.getIntroduce())) {
            sb.append(" and c.introduce like :introduce");
            map.put("introduce", "%" + params.getIntroduce() + "%");
        }
        if (params.getCheckStatus() > 0) {
            sb.append(" and c.checkStatus = :checkStatus");
            map.put("checkStatus", params.getCheckStatus());
        }
        sb.append(" order by c.createTime desc");
        return sb.toString();
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Model model, long id) {
        LiveCheck liveCheck = queryChannelService.findOne(LiveCheck.class, id);
        model.addAttribute("liveCheck", liveCheck);
        return "mall/live/check/detail";
    }


    /**
     * 审核直播间
     */
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    private Map<String, Object> check(HttpServletRequest request, LiveCheck params) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalRequestException("request must be ajax !");
            }
            LiveCheck liveCheck = queryChannelService.findOne(LiveCheck.class, params.getId());
            liveApplication.check(liveCheck, params.getCheckStatus(), params.getCheckNote());
            return ImmutableMap.of("status", true);
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        }catch (Exception e) {
            log.error("[审核直播间异常]", e);
            return ImmutableMap.of("status", false, "message", "审核异常");
        }
    }

    /**
     * 批量审核
     */
    @RequestMapping(value = "/checkAll", method = RequestMethod.PUT)
    @ResponseBody
    public Map<String, Object> checkAll(HttpServletRequest request, String data) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalArgumentException("request must be ajax");
            }
            liveApplication.check(data, 2, "批量审批通过");
            return ImmutableMap.of("status", true, "message", "审核成功");
        } catch (Exception e) {
            log.error("批量审核异常", e);
            return ImmutableMap.of("status", false, "message", "审核异常");
        }
    }



}
